<template>
  <div>
    <div class="coni_header" :class="bg">
      <div class="logo" :style="`width: ${myWidth}px`">
        <img src="../../assets/image/logo@1x.png" alt="" style="width: 15%; height: auto;">
        <span style="position: relative;left:30px; top: -6px;">客户需求管理系统</span>
      </div>
      <slot></slot>
      <div class="my_info">
        <div style="font-size: 14px;">
          <img v-if="!$store.state.module1.UserInfo['avatar']" class="photo" src="../../assets/image/my_photo.png" alt="">
          <img v-else class="photo" :src="$store.state.module1.UserInfo['avatar']" alt="">
          {{$store.state.module1.UserInfo['nickName']}}
          <!-- {{$store.state.module1.UserInfo['sysRole'] && $store.state.module1.UserInfo['sysRole']['roleName'] || ''}} -->
        </div>
        <div class="chang_info">
          <p @click="user_Show = true">个人详情</p>
          <p @click="navigateTo">修改密码</p>
          <p @click="signAut">退出登录</p>
        </div>
      </div>
    </div>

    <el-dialog
        title="个人详情"
        :visible.sync="user_Show"
        width="30%"
        center>
        <div style="font-size: 18px;">
          <span>帐号：</span>      {{$store.state.module1.UserInfo['userName']}}<br>
<!--          <span>角色名称：</span>   {{$store.state.module1.UserInfo['sysRole'] && $store.state.module1.UserInfo['sysRole']['roleName']}}<br>-->
          <span>电话号码：</span>   {{$store.state.module1.UserInfo['phoneNumber']}} <br>
<!--          <span>企业名称：</span>   {{$store.state.module1.UserInfo['sysCompany'] && $store.state.module1.UserInfo['sysCompany']['companyName']}}<br>-->
          <span>邮箱：</span> {{$store.state.module1.UserInfo.email}}<br>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button class="bt-color-tjd" type="primary" @click="user_Show = false">确 定</el-button>
        </span>
      </el-dialog>

<!--     <el-dialog-->
<!--        title="修改密码"-->
<!--        :visible.sync="centerDialogVisible"-->
<!--        width="40%"-->
<!--        center>-->
<!--        <div style="font-size: 16px;">-->
<!--        &lt;!&ndash; <span>原密码：</span> <input v-model="old_password" placeholder="请输入原密码" type="text"> <br> &ndash;&gt;-->
<!--        <span>新密码：</span> <input v-model="new_password" placeholder="请输入新密码" type="password"> <br>-->
<!--        <span>确认新密码：</span> <input v-model="again_password" placeholder="再次确认新密码" type="password">-->
<!--        </div>-->
<!--        <span slot="footer" class="dialog-footer">-->
<!--          <el-button @click="centerDialogVisible = false">取 消</el-button>-->
<!--          <el-button class="bt-color-tjd" type="primary" @click="modifyPassword">确 定</el-button>-->
<!--        </span>-->
<!--      </el-dialog>-->
    <el-dialog title="修改密码" :visible.sync="changePasswordShow" width="40%" center @close="closeDialog">
      <el-form :model="form" :rules="rules" ref="dialogForm">
        <el-form-item label="新密码：" :label-width="formLabelWidth" prop="newPassword">
          <el-input v-model="form.newPassword" autocomplete="off" placeholder="请输入新密码"
                    style="width: 100%;"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码：" :label-width="formLabelWidth" prop="againPassword">
          <el-input v-model="form.againPassword" autocomplete="off" placeholder="再次确认新密码"
                    style="width: 100%;"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button class="bt-color-tjd" type="primary" @click="modifyPassword">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
import { post } from '@/api/my_axios'
import { delCookie, isFormalEnvironment } from '@/utils/aqi'
export default {
  name: '',
  props: {
    bg: {
      type: String,
      default: () => { return '' }
    },
    myWidth: {
      type: Number,
      default: () => { return '' }
    }
  },
  data () {
    return {
      user_Show: false,
      centerDialogVisible: false,
      old_password: '',
      new_password: '',
      again_password: '',
      form: {
        userId: '',
        newPassword: '',
        againPassword: ''
      },
      changePasswordShow: false, // 修改密码
      formLabelWidth: '140px',
      rules: {
        newPassword: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 32, message: '密码必须是6到32个字符之间的字母+数字+符号', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9!@#$%^&*()\-+]+$/,
            message: '密码仅支持数字、字母和特殊符号 (!@#$%^&*()-+)',
            trigger: 'blur'
          },
          {
            validator: (rule, value, callback) => {
              if (!/[A-Za-z]/.test(value)) {
                callback(new Error('密码必须包含字母'))
              } else if (!/[0-9]/.test(value)) {
                callback(new Error('密码必须包含数字'))
              } else if (!/[!@#$%^&*()\-+]/.test(value)) {
                callback(new Error('密码必须包含特殊符号 (!@#$%^&*()-+)'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        againPassword: [{ required: true, message: '必填项不能为空！', trigger: 'change' }]
      }
    }
  },
  mounted () {
    this.$store.dispatch('getUserData')
  },
  methods: {

    modifyPassword () {
      if (this.form.newPassword !== this.form.againPassword) {
        this.$message.error('两次密码不一样！')
        return
      }
      post('/api/crm/sys/manage/crm/user/resetUserPwd',
        { password: this.form.newPassword, userId: this.form.userId }).then(req => {
        if (req && req.code === 200) {
          this.$message({ message: '修改成功！', type: 'success' })
          this.closeDialog()
          this.$router.push('/Login')
          delCookie('token')
        }
      })
    },
    // modifyPassword () {
    //   if (!this.new_password || !this.again_password) {
    //     this.$message.error('密码不能为空！')
    //     return
    //   }
    //   if (this.new_password !== this.again_password) {
    //     this.$message.error('两次密码不一样！')
    //     return
    //   }
    //   if (this.new_password.length < 3) {
    //     this.$message.error('密码太过于简洁！')
    //     return
    //   }
    //   post('/api/crm/sys/manage/crm/user/resetUserPwd', { password: this.new_password, userId: this.$store.state.module1.UserInfo.userId }).then(req => {
    //     if (req && req.code === 200) {
    //       this.$message({ message: '修改成功！', type: 'success' })
    //       this.centerDialogVisible = false
    //       this.$router.push('/Login')
    //       delCookie('token')
    //     }
    //   })
    // },
    signAut () {
      post('/api/crm/sys/manage/token/user/logout').then(req => {
        if (req && req.code === 200) {
          this.$message({ message: '退出成功！', type: 'success' })
          this.$router.push('/Login')
          delCookie('token')
        }
      })
    },

    // 关闭dialog框
    closeDialog () {
      this.$refs.dialogForm.resetFields()
      this.changePasswordShow = false
      Object.keys(this.form).forEach(key => {
        this.form[key] = ''
      })
    },

    // 接口跳转
    navigateTo () {
        this.changePasswordShow = true
        this.form.userId = this.$store.state.module1.UserInfo.userId
    }
  },
  computed: {
    // 海外正式环境
    isFormalEnvironment () {
      return isFormalEnvironment()
    }
  }
}
</script>
<style lang="scss" scoped>
.coni_header{
  position: fixed;
  z-index: 999;
  width: 100%;
  height: 40px;
  -webkit-user-select:none;
  -moz-user-select:none;
  -ms-user-select:none;
  user-select:none;
  background: #fff;
}
.logo {
  float: left;
  width: 194px;
  font-size: 14px;
  line-height: 40px;
  color: #337DF7;
  font-weight: bold;
  border-right: 1px solid #E8ECEF;
  img {
    position: relative;
    left: 18px;
    top: 4px;
  }
}

.my_info {
  position: relative;
  float: right;
  margin-top: 0px;
  margin-right: 40px;
  height: 60px;
  cursor: pointer;
  .photo {
    position: relative;
    width: 28px;
    top: 8px;
    right: 4px;
    background: #fff;
    border-radius: 100%;
    border: none;
  }
  img {
    width: 28px;
    height: 28px;
    border-radius: 100%;
    border: none;
  }
  &:hover .chang_info{
    display: block;
  }
  .chang_info {
    display: none;
    position: absolute;
    z-index: 1000;
    top: 34px;
    right: 0px;
    width: 136px;
    height: 144px;
    text-align: center;
    color: #000;
    background: url(../../assets/image/info-bg.png);
    p:nth-of-type(1) {
      margin-top: 15px;
    }
    p:nth-of-type(2) {
      margin-top: 1px;
    }
    p:nth-of-type(3) {
      margin-top: 3px;
    }
  }
  p {
    width: 104px;
    margin-left: 20px;
    font-size: 12px;
    line-height: 34px;
  }
}
 .el-dialog__body span{
    display: inline-block;
    width: 140px;
    text-align: right;
    margin-bottom: 20px;
  }

  .el-dialog__body input {
    padding-left: 6px;
    width: 250px;
    height: 26px;
    border: 1px solid #666;
    border-radius: 4px;
  }
  .rig-t, .lef-t {
    float: left;
    margin-left: 12px;
    margin-top: 12px;
    cursor: pointer;
  }
</style>
